package com.qding.park.adapter.bsg.adapter.util;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qding.park.adapter.bsg.adapter.entity.ResponseData;

/**
    * @ClassName: ResponseHelper
    * @Description: TODO(应答帮助类)
    * @author Administrator
    * @date 2016年9月8日
    *
    */
public class ResponseHelper {
	
	
	    /**
	    * @Title: createStringXmlResPonse
	    * @Description: TODO(创建string 为根的响应报文)
	    * @param @param data
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    */
	public static String createStringXmlResponse(ResponseData data){
		 Document document = DocumentHelper.createDocument();  
		 document.setXMLEncoding("utf-8");
		 Element root = document.addElement("string");//添加文档根  
		 //设置输出值为null 的属性值
		 root.setText(data==null?"":JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue));
		return document.asXML();
	}
	
	    /**
	    * @Title: createEmptyStringXmlResponseSuccess
	    * @Description: TODO(创建空的成功应答报文)
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    */
	public static String createEmptyStringXmlResponseSuccess(){
		 Document document = DocumentHelper.createDocument();  
		 document.setXMLEncoding("utf-8");
		 Element root = document.addElement("string");//添加文档根  
		 //设置输出值为null 的属性值
		 ResponseData data=new ResponseData();
		 data.setErrorMsg("请求成功");
		 data.setErrorCode(ReportErrorCodeEnum.SUCCESS.getCode());
		 root.setText(data==null?"":JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue));
		return document.asXML();
	}
	
	    /**
	    * @Title: createEmptyStringXmlResponseSuccess
	    * @Description: TODO(指定 guid ,errormsg 创建应答报文)
	    * @param @param guid
	    * @param @param errorMsg
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    */
	public static String createEmptyStringXmlResponseSuccess(String guid,String errorMsg){
		 Document document = DocumentHelper.createDocument();  
		 document.setXMLEncoding("utf-8");
		 Element root = document.addElement("string");//添加文档根  
		 //设置输出值为null 的属性值
		 ResponseData data=new ResponseData();
		 data.setCommandGuid(guid);
		 data.setErrorMsg(errorMsg);
		 data.setErrorCode(ReportErrorCodeEnum.SUCCESS.getCode());
		 root.setText(data==null?"":JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue));
		return document.asXML();
	}
	    /**
	    * @Title: createEmptyStringXmlResponseFail
	    * @Description: TODO(创建空的失败应答报文)
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    */
	public static String createEmptyStringXmlResponseFail(){
		 Document document = DocumentHelper.createDocument();  
		 document.setXMLEncoding("utf-8");
		 Element root = document.addElement("string");//添加文档根  
		 //设置输出值为null 的属性值
		 ResponseData data=new ResponseData();
		 data.setErrorMsg("请求失败");
		 data.setErrorCode(ReportErrorCodeEnum.FAIL.getCode());
		 root.setText(data==null?"":JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue));
		return document.asXML();
	}
	
	    /**
	    * @Title: createEmptyStringXmlResponseFail
	    * @Description: TODO(指定 guid errorMsg 创建应答报文)
	    * @param @param guid
	    * @param @param errorMsg
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    */
	public static String createEmptyStringXmlResponseFail(String guid,String errorMsg){
		 Document document = DocumentHelper.createDocument();  
		 document.setXMLEncoding("utf-8");
		 Element root = document.addElement("string");//添加文档根  
		 //设置输出值为null 的属性值
		 ResponseData data=new ResponseData();
		 data.setCommandGuid(guid);
		 data.setErrorMsg(errorMsg);
		 data.setErrorCode(ReportErrorCodeEnum.FAIL.getCode());
		 root.setText(data==null?"":JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue));
		return document.asXML();
	}
	
	public static void main(String [] args){
//		ResponseData data=new ResponseData();
//		data.setCommandGuid("www");
//		data.setErrorCode("1");
//		data.setErrorMsg("请求成功");
		String result=createEmptyStringXmlResponseFail();
		
		System.out.println(result);
	}
}
